package org.lc.platform.redis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjUtil;
import org.lc.platform.redis.service.CacheService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class CacheServiceImpl implements CacheService {
    final RedisTemplate<Object, Object> _redis;

    public CacheServiceImpl(RedisTemplate<Object, Object> redis) {
        this._redis = redis;
    }


    public RedisTemplate<Object, Object> getRedis() {
        return this._redis;
    }

    @Override
    public void setStr(String key, String value, Long expired) {
        set(key, value, expired);
    }

    @Override
    public void setStr(String key, String value) {
        set(key, value);
    }

    @Override
    public String getStr(String key) {
        return Convert.toStr(get(key));
    }

    @Override
    public void setInt(String key, Integer value, Long expired) {
        set(key, value, expired);
    }

    @Override
    public void setInt(String key, Integer value) {
        set(key, value);
    }

    @Override
    public Integer getInt(String key) {
        var val = get(key);
        if (ObjUtil.isNull(val)) {
            return null;
        }
        return Convert.toInt(val);
    }

    @Override
    public Integer getInt(String key, int defaultValue) {
        var val = get(key);
        if (ObjUtil.isNull(val)) {
            return defaultValue;
        }
        return Convert.toInt(val);
    }

    @Override
    public Map<Object, Object> getMap(String key) {
        return this._redis.opsForHash().entries(key);
    }

    @Override
    public void setFloat(String key, Float value, Long expired) {
        set(key, value, expired);
    }

    @Override
    public void setFloat(String key, Float value) {
        set(key, value);
    }

    @Override
    public Float getFloat(String key) {
        var val = get(key);
        if (ObjUtil.isNull(val)) {
            return null;
        }
        return Convert.toFloat(val);
    }

    @Override
    public Float getFloat(String key, float defaultValue) {
        var val = get(key);
        if (ObjUtil.isNull(val)) {
            return defaultValue;
        }
        return Convert.toFloat(val);
    }

    @Override
    public void set(String key, Object value, Long expired) {
        try {
            this._redis.opsForValue().set(key, value, expired, TimeUnit.SECONDS);
        } catch (Exception e) {
            System.err.println("存储" + value + "异常：" + e.getMessage());
        }
    }

    @Override
    public void set(String key, Object value) {
        try {
            this._redis.opsForValue().set(key, value);
        } catch (Exception e) {
            System.err.println("存储" + value + "异常：" + e.getMessage());
        }
    }

    @Override
    public <T> T get(String key, Class<T> cls) {
        try {
            return BeanUtil.toBean(get(key), cls);
        } catch (Exception e) {
            System.err.println("读取" + key + "异常：" + e.getMessage());
        }
        return null;
    }

    @Override
    public Object get(String key) {
        try {
            return this._redis.opsForValue().get(key);
        } catch (Exception e) {
            System.err.println("读取" + key + "异常：" + e.getMessage());
        }
        return null;
    }

    @Override
    public <T> void setList(String key, List<T> list) {
        try {
            remove(key);
            list.forEach(l -> _redis.opsForList().leftPushAll(key, l));
        } catch (Exception e) {
            System.err.println("存储List异常：" + e.getMessage());
        }
    }

    /**
     * 获取List集合
     */
    @Override
    public <T> List<T> getList(String key) {
        try {
            return (List<T>) this._redis.opsForList().range(key, 0, -1);
        } catch (Exception e) {
            System.err.println("读取List异常：" + e.getMessage());
        }
        return List.of();
    }

    @Override
    public void remove(String key) {
        if (hasKey(key)) {
            _redis.delete(key);
        }
    }

    @Override
    public void remove(String... key) {
        _redis.delete(List.of(key));
    }

    @Override
    public Boolean hasKey(String key) {
        return this._redis.hasKey(key);
    }
}
